package com.funtonow.jqdk.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.funtonow.jqdk.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by zhouyou on 2016/6/27.
 * Class desc: fragment base class
 */
public abstract class BaseFragment extends Fragment {

    private View mView;
    private Toolbar mToolbar;
    private Activity mActivity;
    protected Context mContext;
    protected static String TAG;
    private AppCompatActivity mCompatActivity;
    protected LayoutInflater mLayoutInflater;

    // 是否可见
    protected boolean mIsVisiable;
    // 是否已经调用了 onCreateView
    protected boolean mIsViewCreate;

    public ImageButton mIbReturn;
    private TextView mTvTitle;
    private Unbinder unbinder;
    public ImageButton ibNews;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = this.getClass().getSimpleName();
        mActivity = getActivity();
        mContext = mActivity;
        mCompatActivity = (AppCompatActivity) mActivity;
        setHasOptionsMenu(true);
    }

    @SuppressLint("RestrictedApi")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mLayoutInflater = getLayoutInflater(savedInstanceState);
        mView = getLayout(inflater, container, savedInstanceState);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mIsViewCreate = true;
        // 绑定依赖注入框架
        unbinder = ButterKnife.bind(this, mView);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        onCreateFragment(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    /**
     * 初始化标题栏
     */
    public void initToolbar(String title) {
        mIbReturn = (ImageButton) findViewById(R.id.ib_back);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        ibNews = (ImageButton) findViewById(R.id.ib_news);
        // 设置标题
        if (!TextUtils.isEmpty(title)) {
            mTvTitle.setText(title);
        }

        // 设置左侧图标
        mIbReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBack();
            }
        });
    }

    /**
     * 设置抽屉图片
     *
     * @param drawableId
     */
    public void setDrawable(int drawableId) {
        if (mIbReturn != null) {
            mIbReturn.setImageDrawable(getResources().getDrawable(drawableId));
        }
    }

    /**
     * 设置抽屉图片
     *
     * @param drawableId
     */
    public void setRightDrawable(int drawableId) {
        if (ibNews != null) {
            ibNews.setImageDrawable(getResources().getDrawable(drawableId));
        }
    }

    /**
     * 返回事件，默认退出当前 activity
     */
    protected void onBack(){
        finish();
    }

    /**
     * 销毁当前挂载的 Activity
     */
    protected void finish(){
        mActivity.finish();
    }

    /**
     * 查找当前控件
     */
    protected View findViewById(int id){
        return mView.findViewById(id);
    }

    /**
     * 获取 Layout 布局
     */
    public abstract View getLayout(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mIsVisiable = isVisibleToUser;
        if(mIsVisiable && mIsViewCreate){
            onLazyLoadData();
        }
    }

    protected void onLazyLoadData(){

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public abstract void onCreateFragment(@Nullable Bundle savedInstanceState);
}
